package com.jolink.omiis.wechat;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.joinlink.omiis.commons.persistence.api.IDBProcessor;
import com.joinlink.omiis.commons.persistence.impl.DBProcessor;
import com.jolink.omiis.wechat.api.ICustomerService;
import com.jolink.omiis.wechat.api.IMaterialApi;
import com.jolink.omiis.wechat.api.IRobotApi;
import com.jolink.omiis.wechat.api.ITemplateMessageApi;
import com.jolink.omiis.wechat.api.IUserOperationApi;
import com.jolink.omiis.wechat.api.IWeChatBrokerMessage;
import com.jolink.omiis.wechat.api.impl.CustomerService;
import com.jolink.omiis.wechat.api.impl.MaterialApiImpl;
import com.jolink.omiis.wechat.api.impl.RobotApiImpl;
import com.jolink.omiis.wechat.api.impl.TemplateMessageApiImpl;
import com.jolink.omiis.wechat.api.impl.UserOperationApi;
import com.jolink.omiis.wechat.api.impl.WeChatBrokerMessage;
import com.jolink.omiis.wechat.faq.ExpressionConfiguration;
import com.jolink.omiis.wechat.http.HttpHandler;
import com.jolink.omiis.wechat.http.IHttpHandler;
import com.jolink.omiis.wechat.inner.IOmiisMessager;
import com.jolink.omiis.wechat.inner.OmiisMessager;
import com.jolink.omiis.wechat.inner.OmiisResponseMessageHandler;
import com.jolink.omiis.wechat.mongo.IMongodbProcessor;
import com.jolink.omiis.wechat.mongo.MongodbProcessor;
import com.jolink.omiis.wechat.redis.IRedisProcessor;
import com.jolink.omiis.wechat.redis.JedisMQMessageWorker;
import com.jolink.omiis.wechat.redis.JedisMessageListener;
import com.jolink.omiis.wechat.redis.RedisClusterProcessor;
import com.jolink.omiis.wechat.token.AuthorizerAccessTokenService;
import com.jolink.omiis.wechat.token.IAuthorizerAccessTokenService;

/**
 * Spring配置类，代替配置文件
 * 
 * @author WangYanqing
 *
 */
@Configuration
@EnableScheduling
public class WeChatSpringConfig {

	/**
	 * 创建线程池 线程池参数配置后续需要参数调整
	 * 
	 * @return
	 */
	@Bean(destroyMethod = "shutdown")
	public ThreadPoolTaskExecutor createTaskExecutosr() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(10);
		taskExecutor.setMaxPoolSize(1000);
		return taskExecutor;
	}

	/**
	 * redis操作接口 后续添加读配置文件，支持集群、单机两种模式
	 * 
	 * @return
	 */
	@Bean
	@Scope(scopeName = "prototype")
	public IRedisProcessor createRedisProcessor() {
		IRedisProcessor redisProcessor = new RedisClusterProcessor();
		return redisProcessor;
	}

	/**
	 * 创建WeChatContext（上下文）
	 * 
	 * @return
	 */
	@Bean(name = "context", initMethod = "initialize", destroyMethod = "shutdown")
	public WeChatContext createWeChatContext() {
		WeChatContext wechatContext = new WeChatContext();
		return wechatContext;
	}

	@Bean
	public OmiisResponseMessageHandler createOmiisResponseMessageHandler() {
		return new OmiisResponseMessageHandler();
	}

	/**
	 * 全媒体通讯接口
	 * 
	 * @return
	 */
	@Bean(destroyMethod = "disconnect")
	public IOmiisMessager createOmiisMessager(OmiisResponseMessageHandler omiisResponseMessageHandler) {
		IOmiisMessager messager = new OmiisMessager();
		messager.registerMessageListener(omiisResponseMessageHandler);
		return messager;
	}

	/**
	 * 中继消息通讯接口
	 * 
	 * @return
	 */
	@Bean
	@Scope(scopeName = "prototype")
	public IWeChatBrokerMessage createBroker() {
		IWeChatBrokerMessage broker = new WeChatBrokerMessage();
		return broker;
	}

	/**
	 * 微信用户管理接口
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Bean
	@Scope(scopeName = "prototype")
	public IUserOperationApi createUserOperationApi() {
		return new UserOperationApi();
	}

	/**
	 * 机器人API
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Bean
	@Scope(scopeName = "prototype")
	public IRobotApi createRobotApi() {
		return new RobotApiImpl();
	}

	/**
	 * 客服消息接口
	 * 
	 * @return
	 */
	@Scope(scopeName = "prototype")
	@Bean
	public ICustomerService createCSApi() {
		return new CustomerService();
	}

	/**
	 * 腾讯素材管理接口
	 * 
	 * @return
	 */
	@Bean
	@Scope(scopeName = "prototype")
	public IMaterialApi createMaterialApi() {
		return new MaterialApiImpl();
	}

	/**
	 * 公众号Token服务
	 * 
	 * @return
	 */
	@Bean
	public IAuthorizerAccessTokenService createAuthorizerAccessTokenService() {
		return new AuthorizerAccessTokenService();
	}

	/**
	 * 模板消息接口
	 * 
	 * @return
	 */
	@Bean
	public ITemplateMessageApi createTemplateMessageApi() {
		return new TemplateMessageApiImpl();
	}

	/**
	 * Redis消息接口
	 * 
	 * @return
	 */
	@Bean
	@Scope(scopeName = "prototype")
	public IMongodbProcessor createMongodbProcessor() {
		return new MongodbProcessor();
	}

	@SuppressWarnings("rawtypes")
	@Bean
	@Scope(scopeName = "prototype")
	public IDBProcessor createDBProcessor() {
		return new DBProcessor<>();
	}

	@Bean
	@Scope(scopeName = "prototype")
	public IHttpHandler<?> createHttpHandler() {
		return new HttpHandler<>();
	}

	@Bean
	public ExpressionConfiguration createExpressionConfiguration() {
		return new ExpressionConfiguration();
	}

	@Bean
	public JedisMessageListener createJedisMessageListener() {
		return new JedisMessageListener();
	}

	@Bean
	public JedisMQMessageWorker createJedisMQMessageWorker() {
		return new JedisMQMessageWorker();
	}
}
